﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CFSystem.JXC.Model;
using CFSystem.JXC.BLL;

namespace CFSystem.JXC.BasisInfo
{
    public partial class FrmVendorDetailsEdit : Form
    {

        private VendorInfo  _vendor;
        private Vendor  _vendorBLL;

        public delegate void UpdateDataBinding();
        public event UpdateDataBinding SyncDataGridView;

        private bool _isDataValid = true;
        private bool _isadded = false;
        
        public FrmVendorDetailsEdit()
        {
            InitializeComponent();
            _vendor = new VendorInfo();
            _vendorBLL = new Vendor();

            AddChangeEvent();

            btnApply.Enabled = false;

            _isadded = true;
        }

        public FrmVendorDetailsEdit(int vendorID)
            : this()
        {
            
            _vendor   =  _vendorBLL.GetVendorByID(vendorID);

            GenerateFormData();

            _isadded = false;
        }

        /// <summary>
        /// 加载行数据
        /// </summary>
        private void GenerateFormData()
        {
            txtAccount.Text  = _vendor.Account;
            txtAddress.Text = _vendor.Address;
            txtContact.Text = _vendor.Contact;
            txtEmail.Text = _vendor.Email;
            txtMemo.Text = _vendor.Memo;
            txtPhoneNo.Text = _vendor.PhoneNo;
            txtVendorName.Text = _vendor.VendorName;
        }
       

        private void FrmVendorDetailsEdit_Load(object sender, EventArgs e)
        {

        }

        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void AddChangeEvent()
        {
            foreach (Control control in this.grpVendorInfo.Controls)
            {
                if (control is TextBox || control is RichTextBox)
                {
                    control.TextChanged += new EventHandler(control_TextChanged);
                }
            }
        }

        void control_TextChanged(object sender, EventArgs e)
        {
            btnApply.Enabled = true;
        }
        /// <summary>
        /// Save data to db and refresh the grid display.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnConfirm_Click(object sender, EventArgs e)
        {
            if (btnApply.Enabled)
            {
               GenerateVendorData();
                try
                {
                    if (_isadded)
                    {
                        _vendorBLL.Insert(_vendor);
                        MessageBox.Show("添加数据成功!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        _vendorBLL.Update(_vendor);
                        MessageBox.Show("更新数据成功!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    SyncDataGridView();
                    this.Close();
                }
                catch (Exception ex)
                {
                    if (_isadded)
                    {
                        MessageBox.Show("添加数据失败!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    else
                    {
                        MessageBox.Show("更新数据失败!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
            else
            {
                this.Close();
            }
        }
        /// <summary>
        /// 
        /// </summary>
        private void GenerateVendorData()
        {
            try
            {
                _vendor.VendorName =Common.CheckIsEmpty(label1.Text, txtVendorName.Text.ToString());
                _vendor.Contact = txtContact.Text.ToString();
                _vendor.Email = txtEmail.Text.ToString();
                _vendor.Address = txtAddress.Text.ToString();
                _vendor.Memo = txtMemo.Text.ToString();
                _vendor.Account = txtAccount.Text.ToString();
                _vendor.PhoneNo = txtPhoneNo.Text.ToString();
            }
            catch (Exception ex)
            {
                _isDataValid = false;
                MessageBox.Show(ex.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            _vendor  = null;
            this.Close();
        }

        private void btnApply_Click(object sender, EventArgs e)
        {
            GenerateVendorData();
            if (_isDataValid)
            {
                try
                {
                    if (_isadded)
                    {
                        _vendorBLL.Insert(_vendor);
                        MessageBox.Show("添加数据成功!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        _vendorBLL.Update(_vendor);
                        MessageBox.Show("更新数据成功!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    btnApply.Enabled = false;
                    SyncDataGridView();
                }
                catch (Exception ex)
                {
                    if (_isadded)
                    {
                        MessageBox.Show("添加数据失败!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    else
                    {
                        MessageBox.Show("更新数据失败!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
        }
    }
}
